<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>绿色出行积分系统</h2>
        <p>碳中和主题城市出行平台</p>
      </div>
      
      <form class="login-form" @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="phone">手机号</label>
          <input 
            id="phone" 
            type="tel" 
            v-model="loginForm.phone" 
            placeholder="请输入手机号"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="verificationCode">验证码</label>
          <div class="verification-code">
            <input 
              id="verificationCode" 
              type="text" 
              v-model="loginForm.verificationCode" 
              placeholder="请输入验证码"
              required
            />
            <button 
              type="button" 
              class="send-code-btn" 
              :disabled="isSending || countdown > 0"
              @click="sendVerificationCode"
            >
              {{ getCodeButtonText }}
            </button>
          </div>
        </div>
        
        <button type="submit" class="login-btn" :disabled="isLoading">
          {{ isLoading ? '登录中...' : '登录' }}
        </button>
      </form>
      
      <div class="login-footer">
        <p>登录即表示同意《用户协议》和《隐私政策》</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

// 表单数据
const loginForm = ref({
  phone: '',
  verificationCode: ''
})

// 状态管理
const isLoading = ref(false)
const isSending = ref(false)
const countdown = ref(0)

// 获取验证码按钮文字
const getCodeButtonText = computed(() => {
  if (isSending.value) return '发送中...'
  if (countdown.value > 0) return `${countdown.value}秒后重发`
  return '获取验证码'
})

// 发送验证码
const sendVerificationCode = async () => {
  if (!loginForm.value.phone) {
    alert('请输入手机号')
    return
  }
  
  // 验证手机号格式
  const phoneRegex = /^1[3-9]\d{9}$/
  if (!phoneRegex.test(loginForm.value.phone)) {
    alert('请输入正确的手机号')
    return
  }
  
  isSending.value = true
  
  try {
    // 模拟发送验证码API调用
    await new Promise(resolve => setTimeout(resolve, 1000))
    console.log(`验证码已发送至手机: ${loginForm.value.phone}`)
    
    // 启动倒计时
    startCountdown()
  } catch (error) {
    console.error('发送验证码失败:', error)
    alert('发送验证码失败，请稍后重试')
  } finally {
    isSending.value = false
  }
}

// 启动倒计时
const startCountdown = () => {
  countdown.value = 60
  const timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
    }
  }, 1000)
}

// 处理登录
const handleLogin = async () => {
  if (!loginForm.value.phone || !loginForm.value.verificationCode) {
    alert('请填写完整信息')
    return
  }
  
  // 验证手机号格式
  const phoneRegex = /^1[3-9]\d{9}$/
  if (!phoneRegex.test(loginForm.value.phone)) {
    alert('请输入正确的手机号')
    return
  }
  
  // 验证验证码格式（假设为6位数字）
  const codeRegex = /^\d{6}$/
  if (!codeRegex.test(loginForm.value.verificationCode)) {
    alert('请输入正确的验证码')
    return
  }
  
  isLoading.value = true
  
  try {
    // 模拟登录API调用
    await new Promise(resolve => setTimeout(resolve, 1500))
    console.log('登录成功', loginForm.value)
    // 这里应该跳转到主页面
    alert('登录成功!')
  } catch (error) {
    console.error('登录失败:', error)
    alert('登录失败，请稍后重试')
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  padding: 40px 30px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  margin-bottom: 10px;
  font-size: 24px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form {
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  box-sizing: border-box;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #4facfe;
}

.verification-code {
  display: flex;
  gap: 10px;
}

.verification-code input {
  flex: 1;
}

.send-code-btn {
  padding: 12px 15px;
  background: linear-gradient(to right, #4facfe, #00f2fe);
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
  transition: opacity 0.3s;
}

.send-code-btn:hover:not(:disabled) {
  opacity: 0.9;
}

.send-code-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.login-btn {
  width: 100%;
  padding: 15px;
  background: linear-gradient(to right, #4facfe, #00f2fe);
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: opacity 0.3s;
}

.login-btn:hover:not(:disabled) {
  opacity: 0.9;
}

.login-btn:disabled {
  cursor: not-allowed;
  opacity: 0.7;
}

.login-footer {
  text-align: center;
  font-size: 12px;
  color: #999;
}

.login-footer a {
  color: #4facfe;
  text-decoration: none;
}

.login-footer a:hover {
  text-decoration: underline;
}
</style>